var contacts = {};
var chats = {};
userId = sessionStorage.getItem("user");
// userId = "000003";
var online = 0;
//ip = "http://10.210.70.115:80/";
//ip = "http://10.103.240.160:8080/";
ip = "http://10.103.240.160:8080/";

function getContactsData() {
	$.ajax({
		// async: false,
		url: ip + "fetchFriendsList",
		type: "get",
		datatype: "json",
		contentType: "application/json; charset=utf-8",
		data: {
				'uid': userId
			},
		success: function(data) {
            data = JSON.parse(data)

			for(var i=0; i<data.length; ++i) {
                var user = data[i];
                var uid = 'u' + user['id'];
                var cont = {
                    "nickname": user['name'],
                    "username": user['name'], 
                    "photo": user['avatar'],
                    "department": user['department'],
                    "phone": user['phone'],
                    "email": user['email'],
                    "ID": user['id'],
                    "dialogType": "single"
                };

                contacts[uid] = cont;
            }

		},
		
	// {"nickname":"张飞","photo":"images/1.jpg","userName":"等风来","department":"研发部","ID":"23189786", "dialogType": "single"}
	// [{'id':'x','name':'xxx','department':'yyy','phone':'zzzzzzzzzzz','email','xxx'}]

		error: function(xhr, msg) {
			
		}	
	});
	
	$.ajax({
		// async: false,
		url: ip + "getGroupListOfUser",
		type: "get",
		datatype: "json",
		data: {
				'uid': userId
			},
		contentType: "application/json; charset=utf-8",
		success: function(data) {
			data = JSON.parse(data);

			for(var i=0; i<data.length; ++i) {
				var group = data[i];
				var gid = 'g' + group['groupid'];
				var cont = {
					"nickname": group['groupname'],
					"username": group['groupname'], ///////////////////
					"department": "暂无", ////////////////////////////
					"photo": group['groupavatar'],
					"manager": group['manager'],
					"ID": group['groupid'],
					"dialogType": "multi"
				}

				contacts[gid] = cont
			}

            getChatsData();
		},
		
		// {"nickname":"张飞","photo":"images/1.jpg","userName":"等风来","department":"研发部","ID":"23189786", "dialogType": "single"}
		// {"groupid":"1","groupname":"群组1","groupavatar":"NO_AVATAR","manager":"NO_MANAGER"}
		
		error: function() {
			
		}
	});
}

function getChatsData() {
	var csize = Object.keys(contacts).length;
	if(csize <= 20) {
		chats = contacts;
	} else {
		var cnt = 20;
		for(var k in contacts) {
			if(!cnt) break;
			
			chats[k] = contacts[k];
			
			cnt -= 1;
		}
	}

    ordered_chats = Object.keys(chats)

    documentReady();
}

function getUserInfo() {
	$.ajax({
		url: ip + "fetchUserInfo",
        type: "get",
        datatype: "json",
        data: {
            'uid': userId
        },
        contentType: "json",
		success: function(data) {
			data = JSON.parse(data);
			var uid = 'u' + data['id'];
			contacts[uid] = {
				'nickname': data['name'],
				'photo': data['avatar'],
				'userName': data['name'],
				'department': data['department'],
				'ID': data['id'],
				'dialogType': 'single',
				'phone': data['phone'],
				'email': data['email']
                // {"nickname":"张飞","photo":"images/1.jpg","userName":"等风来","department":"研发部","ID":"23189786", "dialogType": "single"}
            // {"id":"2","name":"wanghengchao","avatar":"haha","department":"技术部门","phone":"16666666666","email":"737720233@qq.com"}
			}

			showUserInfo();
		},

		error: function() {

		}
    })
}

(function _init_() {
	getUserInfo();
	getContactsData();
})();